package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.mapper.UserShopMapper;
import com.sky.service.UserShopService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserShopServiceImpl implements UserShopService {

    @Autowired
    private UserShopMapper userShopMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //查询自己购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = userShopMapper.list(shoppingCart);

        //判断当前商品是否存在于购物车
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //如果当前商品存在,修改数量+1
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            userShopMapper.updateNumberById(shoppingCart1);
        } else {
            //如果当前商品不存在,插入数据 数量为1
            if (shoppingCartDTO.getDishId() != null) {
                //插入菜品数据
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //插入套餐数据
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //公共数据
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            userShopMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = userShopMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 删除购物车
     */
    @Override
    public void delete() {
        Long userId = BaseContext.getCurrentId();
        userShopMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车一条数据
     *
     * @param shoppingCartDTO
     */
    @Override
    public void shoppingCartSub(ShoppingCartDTO shoppingCartDTO) {
        //获取用户购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = userShopMapper.list(shoppingCart);
        ShoppingCart userShoppingCart = shoppingCartList.get(0);

        //判断购物车商品数量
        if (userShoppingCart.getNumber() == 1) {
            //如果为1直接删除该购物车记录
            userShopMapper.deleteById(userShoppingCart.getId());
        } else{
            //如果不为1将数量-1
            userShoppingCart.setNumber(userShoppingCart.getNumber() - 1);
            userShopMapper.updateNumberById(userShoppingCart);
        }

    }
}
